package com.cwt_taxi_xa;

import android.os.Bundle;
import android.widget.TextView;

import com.cwt_taxi_xa.model.LocalInfo;
import com.cwt_taxi_xa.util.DateUtil;
import com.cwt_taxi_xa.util.DecimalUtil;
import com.cwt_taxi_xa.util.Tools;

import java.math.BigDecimal;

public class CheckOutActivity extends CWTBaseActivity {

	private static final String TAG = "CheckOutActivity";
	private LocalInfo info;
	private TextView tv_star_time,tv_end_time,tv_driver_code,tv_time,
			tv_value_k,tv_times_all,tv_mileage_business_all,tv_mileage_all,
			tv_amount,tv_times,tv_times_card,tv_amount_card,tv_mileage_sub,
			tv_mileage_business_sub,tv_mileage_banjian;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_check_out);
		setupViews();
		initData();
	}

	private void initData() {
		// TODO Auto-generated method stub
		info = (LocalInfo) getIntent().getSerializableExtra("info");
		if(info == null){
			return;
		}
		String data = info.getData();
		byte[] dataContent = Tools.yxReversal(Tools.parseHexStr2Byte(data));

		int num = 0;
		num += 13+25;

		// 单位代码 BCD[16]
		String companyCode = Tools.byteToString(dataContent, num, 16);
		num += 16;
		// 司机代码 BCD[19] 车辆自编号+司机编码，6位，不足的前面补0
		String driverCode = Tools.byteToString(dataContent, num, 19);
		num += 19;
		// 车牌号 BYTE[6] 车辆自编号，5位，前面补0，ASCII字符
		num += 6;
		// 计价器K值 BCD[2] 格式为XXXX，最大9999，暂时填0
		String valueK = ""+Integer.parseInt(Tools.bcd2Str(Tools.byteTobyte(dataContent, num, 2)));
		num += 2;
		// 当班开机时间 BCD[6] YYYY-MM-DD-hh-mm
		String stime = Tools.bcd2Str(Tools.byteTobyte(dataContent, num, 6));
		num += 6;
		// 当班关机时间 BCD[6] YYYY-MM-DD-hh-mm
		String etime = Tools.bcd2Str(Tools.byteTobyte(dataContent, num, 6));
		num += 6;
		// 当班公里 BCD[3] 格式为XXXXX.X公里
		String dbgl = Tools.bcd2Str(Tools.byteTobyte(dataContent, num, 3));
		num += 3;
		// 当班营运公里 BCD[3] 格式为XXXXX.X公里
		String dbyygl = Tools.bcd2Str(Tools.byteTobyte(dataContent, num, 3));
		num += 3;
		// 车次 BCD[2] 格式为XXXX，最大9999（保留，填0）
		int count = Integer.parseInt(Tools.bcd2Str(Tools.byteTobyte(dataContent, num, 2)));
		num += 2;
		// 计时时间 BCD[3] hh-mm-ss（总计费时间），是部标内容，有业务需求，请确认能否上传
		String jssj = Tools.bcd2Str(Tools.byteTobyte(dataContent, num, 3));
		num += 3;
		// 总计金额 BCD[3] 格式XXXXX.X元
		String amount = Tools.bcd2Str(Tools.byteTobyte(dataContent, num, 3));
		num += 3;
		// 卡收金额 BCD[3] 格式XXXXX.X元
		String ksje = Tools.bcd2Str(Tools.byteTobyte(dataContent, num, 3));
		num += 3;
		// 卡次 BCD[2] 袼式为XXKX，最大9999
		int countCard = Integer.parseInt(Tools.bcd2Str(Tools.byteTobyte(dataContent, num, 2)));
		num += 2;
		// 班间公里 BCD[2] 格式XXX.X公里（上一班签退到本班签 到的公里数）
		String bjgl = Tools.bcd2Str(Tools.byteTobyte(dataContent, num, 2));
		num += 2;
		// 总计公里 BCD[4] 格式为XXXXX.X公里（计价器安装后累积）
		String zjgl = Tools.bcd2Str(Tools.byteTobyte(dataContent, num, 4));
		num += 4;
		// 总营运公里 BCD[4] 格式为XXXXX.X公里（计价器安装后累积）
		String zyygl = Tools.bcd2Str(Tools.byteTobyte(dataContent, num, 4));
		num += 4;
		// 单价 BCD[2] 格式XX.XX元
		num += 2;
		// 总营运次数 UINT32 高位在前，低位在后
		int countAll = Integer.parseInt(Tools.parseByte2HexStr(Tools.byteTobyte(dataContent, num, 4)), 16);
		num += 4;


		stime = DateUtil.getTime(stime, DateUtil.df2, DateUtil.df11);
		etime = DateUtil.getTime(etime, DateUtil.df2, DateUtil.df11);
		tv_end_time.setText(etime);
		tv_star_time.setText(stime);

		BigDecimal decimal = new BigDecimal(amount);
		decimal = decimal.divide(new BigDecimal(10));
		double subAmount = decimal.doubleValue();
		tv_amount.setText(DecimalUtil.priceFormat(subAmount));

		tv_driver_code.setText(driverCode);
		jssj = DateUtil.getTime(jssj, DateUtil.df19, DateUtil.df12);
		tv_time.setText(jssj);
		tv_value_k.setText(valueK);
		tv_times_all.setText(""+countAll);
		tv_times.setText(""+count);
		tv_times_card.setText(""+countCard);
		BigDecimal decimal2 = new BigDecimal(ksje);
		decimal2 = decimal2.divide(new BigDecimal(10));
		double ksjeD = decimal2.doubleValue();
		tv_amount_card.setText(DecimalUtil.priceFormat(ksjeD));
		if(Integer.parseInt(zyygl) == 0){
			zyygl = "0.00";
		}else{
			BigDecimal bigDecimal = new BigDecimal(zyygl);
			bigDecimal = bigDecimal.divide(new BigDecimal(10));
			zyygl = DecimalUtil.priceFormat(bigDecimal.doubleValue());
		}
		tv_mileage_business_all.setText(zyygl);
		if(Integer.parseInt(zjgl) == 0){
			zjgl = "0.00";
		}else{
			BigDecimal bigDecimal = new BigDecimal(zjgl);
			bigDecimal = bigDecimal.divide(new BigDecimal(10));
			zjgl = DecimalUtil.priceFormat(bigDecimal.doubleValue());
		}
		tv_mileage_all.setText(zjgl);
		if(Integer.parseInt(dbgl) == 0){
			dbgl = "0.00";
		}else{
			BigDecimal bigDecimal = new BigDecimal(dbgl);
			bigDecimal = bigDecimal.divide(new BigDecimal(10));
			dbgl = DecimalUtil.priceFormat(bigDecimal.doubleValue());
		}
		tv_mileage_sub.setText(dbgl);
		if(Integer.parseInt(dbyygl) == 0){
			dbyygl = "0.00";
		}else{
			BigDecimal bigDecimal = new BigDecimal(dbyygl);
			bigDecimal = bigDecimal.divide(new BigDecimal(10));
			dbyygl = DecimalUtil.priceFormat(bigDecimal.doubleValue());
		}
		tv_mileage_business_sub.setText(dbyygl);
		if(Integer.parseInt(bjgl) == 0){
			bjgl = "0.00";
		}else{
			BigDecimal bigDecimal = new BigDecimal(bjgl);
			bigDecimal = bigDecimal.divide(new BigDecimal(10));
			bjgl = DecimalUtil.priceFormat(bigDecimal.doubleValue());
		}
		tv_mileage_banjian.setText(bjgl);
	}

	private void setupViews() {
		// TODO Auto-generated method stub
		tv_star_time = (TextView)this.findViewById(R.id.tv_star_time);
		tv_end_time = (TextView)this.findViewById(R.id.tv_end_time);
		tv_driver_code = (TextView)this.findViewById(R.id.tv_driver_code);
		tv_time = (TextView)this.findViewById(R.id.tv_time);
		tv_value_k = (TextView)this.findViewById(R.id.tv_value_k);

		tv_times_all = (TextView)this.findViewById(R.id.tv_times_all);
		tv_mileage_business_all = (TextView)this.findViewById(R.id.tv_mileage_business_all);
		tv_mileage_all = (TextView)this.findViewById(R.id.tv_mileage_all);
		tv_amount = (TextView)this.findViewById(R.id.tv_amount);
		tv_times = (TextView)this.findViewById(R.id.tv_times);
		tv_times_card = (TextView)this.findViewById(R.id.tv_times_card);
		tv_amount_card = (TextView)this.findViewById(R.id.tv_amount_card);
		tv_mileage_sub = (TextView)this.findViewById(R.id.tv_mileage_sub);
		tv_mileage_business_sub = (TextView)this.findViewById(R.id.tv_mileage_business_sub);
		tv_mileage_banjian = (TextView)this.findViewById(R.id.tv_mileage_banjian);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		info = null;
	}

	@Override
	public void setStatus(int index, boolean status) {
		// TODO Auto-generated method stub

	}

	@Override
	public void sendMsg(int type, Object obj) {
		// TODO Auto-generated method stub

	}
}
